package com.arunsk.log4metrics.custom;

public final class Watcher {

	private static class ThreadLocalCounter extends ThreadLocal<Integer> {

		public void decrement(Integer n) {
			increment(-n);
		}

		public void increment(Integer n) {
			set(get().intValue() + n);
		}

		public boolean isZero() {
			return get().intValue() == initialValue();
		}
		
		@Override
		protected Integer initialValue() {
			return Integer.valueOf(0);
		}
	}

	private static final Watcher WATCHER = new Watcher();
	private final ThreadLocalCounter nested;
	
	private Watcher() {
		nested = new ThreadLocalCounter ();
	}

	public static Watcher getInstance() {
		return WATCHER;
	}
	
	public boolean isZero () {
		return nested.isZero();
	}
	
	public void inc () {
		nested.increment(1);
	}
	
	public void dec () {
		nested.decrement(1);
	}
}